/*
 * @Author: wangtao
 * @Date: 2020-09-07 16:52:32
 * @LastEditors: kanglang
 * @LastEditTime: 2021-04-28 18:17:03
 * @Description: 车主端我的车辆列表
 */
import React from 'react';
import {
  View,
  StyleSheet,
  Image,
  Text,
  SafeAreaView,
  TouchableOpacity,
  ScrollView,
  InteractionManager,
  DeviceEventEmitter,
} from 'react-native';
import {
  msg,
  Button,
  XMHeader,
  BackImageOn,
  Loading,
} from '@/common';
import {
  isAndroid,
  px2dp,
  screenWidth,
  mainBgColorWhite,
  fontColorCoffee,
} from '@/styles';
import supply from '@/api/supply';
import { empty04 } from '@/images';

import CarList from './components/car-list';

const { Submit } = Button;
export default class MyTruckList extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      carList: [], // 车辆列表
      loaidng: false,
    };
  }

  componentDidMount() {
    const { navigation } = this.props;
    const state = navigation.state || {};
    const params = state.params || {};
    const { routerFrom } = params;
    this.setState({ routerFrom });

    this.init();
  }

  render() {
    const { carList, loaidng } = this.state;
    return (
      <SafeAreaView style={styles.container}>
        <XMHeader
          title='我的车辆'
          renderLeft={() => {
            return (
              <BackImageOn onClick={() => {
                this.goBack();
              }}
              />

            );
          }}
          renderRight={() => {
            return (
              <TouchableOpacity
                activeOpacity={0.8}
                style={{ marginRight: 14 }}
                onPress={() => {
                  msg.emit('router: goToNext', { routeName: 'TruckCert', type: 'add' });
                }}
              >
                {carList.length > 0 && <Text style={{ color: fontColorCoffee, fontSize: px2dp(32) }}>
                  + 新增车辆
                </Text>}
              </TouchableOpacity>
            );
          }}
        />
        {
          !loaidng ? <CarList carList={carList} /> : <Loading />
        }
      </SafeAreaView>
    );
  }

  //   初始化
  init = () => {
    this.setState({ loaidng: true });
    supply.getCarList().then((res) => {
      this.setState({ loaidng: false });
      if (res.success) {
        this.setState({ carList: res.data });
      } else {
        msg.emit('app:tip', { text: res.msg });
      }
    });
  };

  goBack = () => {
    const { routerFrom } = this.state;
    if (routerFrom) {
      msg.emit('router: refreshRoute', {
        routeName: routerFrom,
      });
      msg.emit('router: goToNext', { routeName: routerFrom });
    } else {
      msg.emit('router: back');
    }
  }

  //
  _renderItem = (item) => {
    return <CarItem item={item} />;
  };
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: mainBgColorWhite,
  },
});
